package com.sunlee.common.core.exception.util;

import com.sunlee.common.core.enums.ErrorCodeEnum;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Exception工具类
 *
 * @author sunlee
 */
public class ExceptionUtils {

    private ExceptionUtils() {
    }

    /**
     * 获取异常信息
     *
     * @param ex 异常
     * @return 返回异常信息
     */
    public static String getErrorStackTrace(Exception ex) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        ex.printStackTrace(pw);
        try {
            pw.close();
            sw.close();
        } catch (Exception e) {
            return ErrorCodeEnum.STREAM_CLOSE_EXCEPTION.getMsg() + sw;
        }

        return sw.toString();
    }
}